package com.tsintergy.tmosguide.datamng.web.base.controller;

import com.tsieframework.core.base.dao.DataPackage;
import com.tsieframework.core.base.dao.jpa.query.JpaWrappers;
import com.tsieframework.core.base.web.BaseResp;
import com.tsieframework.core.base.web.BaseRespBuilder;
import com.tsintergy.tmosguide.datamng.core.DatamngConfigConstants;
import com.tsintergy.tmosguide.datamng.serviceapi.pmm.api.DmgPmmMarketUnitEntityService;
import com.tsintergy.tmosguide.datamng.serviceapi.pmm.pojo.DmgPmmMarketUnitDO;
import com.tsintergy.tmosguide.datamng.web.base.controller.request.unitmng.QueryRequest;
import com.tsintergy.tmosguide.datamng.web.base.controller.request.unitmng.QueryRequestWithVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "机组基本信息管理", tags = "机组基本信息管理")
@RestController
@RequestMapping(DatamngConfigConstants.MAPPING_PREFIX + "/unitmng")
public class UnitmngController {

    @Autowired
    DmgPmmMarketUnitEntityService dmgPmmMarketUnitEntityService;

    @ApiOperation(value = "查询机组列表")
    @GetMapping(value = "/query")
    public BaseResp<DataPackage<DmgPmmMarketUnitDO>> query(QueryRequest request) {
        return BaseRespBuilder.success().setData(
                dmgPmmMarketUnitEntityService.findAllByQueryWrapper(
                        JpaWrappers.<DmgPmmMarketUnitDO>lambdaQuery()
                                .like(
                                    StringUtils.isNotEmpty(request.getUnitName()), 
                                    DmgPmmMarketUnitDO::getName, 
                                    request.getUnitName()
                                ),
                        request.resolvePageable()
                )
        ).build();
    }

    @ApiOperation(value = "保存或更新机组信息")
    @PostMapping(value = "/saveOrUpdate")
    public BaseResp<DmgPmmMarketUnitDO> saveOrUpdate(@RequestBody DmgPmmMarketUnitDO unit) {
        return BaseRespBuilder
                .success()
                .setData(dmgPmmMarketUnitEntityService.saveOrUpdate(unit))
                .build();
    }

    @ApiOperation(value = "删除机组信息")
    @DeleteMapping(value = "/delete")
    public BaseResp<Void> delete(String id){
        dmgPmmMarketUnitEntityService.deleteById(id);
        return BaseRespBuilder.success().build();
    }

    @ApiOperation(value = "查询机组列表-带版本号")
    @GetMapping(value = "/queryWithVersion")
    public BaseResp<DataPackage<DmgPmmMarketUnitDO>> queryWithVersion(QueryRequestWithVersion request) {
        return BaseRespBuilder.success().setData(
                dmgPmmMarketUnitEntityService.findAllByQueryWrapperWithVersion(
                        JpaWrappers.<DmgPmmMarketUnitDO>lambdaQuery()
                                .like(StringUtils.isNotEmpty(request.getUnitName()), DmgPmmMarketUnitDO::getName, request.getUnitName()),
                        request.resolvePageable(),
                        request.getVersion()
                )
        ).build();
    }


}
